using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BlogEngine.Core;
using BlogEngine.Core.Provider;
using BlogEngine.Core.Provider.Interfaces;

namespace BlogEngine.Core.Provider.MSSQL
{
	/// <summary>
	/// Microsoft SQL Server Implementation of BlogProvider
	/// </summary>
	public class CIPhotoEffects : IPhotoEffects
	{

		/// <summary>
		/// Gets a PhotoEffect based on a Guid
		/// </summary>
		/// <param name="id">The PhotoEffect's Guid.</param>
		/// <returns>A matching PhotoEffect</returns>
		public  PhotoEffect Select(Guid id)
		{
			PhotoEffect photoeffect = new PhotoEffect();
			SQLManagedConnection myConn = SQLManagedConnection.GetConnection(Provider.DatabaseName);

			try
			{
				myConn.GetTextCommand("SELECT Title, Effect, DateModified FROM be_PhotoEffects WHERE PhotoEffectID = @id");
				myConn.AddInParameter("@id", DbType.Guid, id);

				using (IDataReader rdr = myConn.ExecuteReader())
				{
					rdr.Read();

					photoeffect.Id = id;

					if (!rdr.IsDBNull(0))
						photoeffect.Title = rdr.GetString(0);
					if (!rdr.IsDBNull(1))
						photoeffect.PhotoEffectName = rdr.GetString(1);
					if (!rdr.IsDBNull(2))
						photoeffect.DateModified = rdr.GetDateTime(2);

					rdr.Close();
				}

			}
			finally
			{
				myConn.CloseIfNonTransactional();
			}

			// Parameters
			try
			{
				myConn.GetTextCommand("SELECT Parameter, Value FROM be_PhotoEffectParameters WHERE PhotoEffectID = @id");
				myConn.AddInParameter("@id", DbType.Guid, id);

				using (IDataReader rdr = myConn.ExecuteReader())
				{
					while (rdr.Read())
						photoeffect.Parameters.Set(rdr.GetString(0), rdr.IsDBNull(1) ? string.Empty : rdr.GetString(1));

					rdr.Close();
				}

			}
			finally
			{
				myConn.CloseIfNonTransactional();
			}

			return photoeffect;
		}

		/// <summary>
		/// Inserts a PhotoEffect
		/// </summary>
		/// <param name="link">Must be a valid link object.</param>
		public  void Insert(PhotoEffect photoeffect)
		{
			if (photoeffect == null)
				throw new ArgumentNullException("photoeffect");

			SQLManagedConnection myConn = new SQLManagedConnection(Provider.DatabaseName);

			try
			{
				PhotoEffect.Items.Add(photoeffect);
				PhotoEffect.Items.Sort();

				myConn.GetTextCommand("INSERT INTO be_PhotoEffects (PhotoEffectID, Title, Effect, DateModified) VALUES (@id, @title, @effect, @modified)");
				myConn.AddInParameter("@id", DbType.Guid, photoeffect.Id);
				myConn.AddInParameter("@title", DbType.String, photoeffect.Title);
				myConn.AddInParameter("@effect", DbType.String, photoeffect.PhotoEffectName);
				myConn.AddInParameter("@modifed", DbType.DateTime, photoeffect.DateModified);

				myConn.ExecuteNonQuery();

				photoeffect.MarkOld();
			}
			finally
			{
				myConn.CloseIfNonTransactional();
			}
		}

		/// <summary>
		/// Updates a PhotoEffect
		/// </summary>
		/// <param name="link">Must be a valid PhotoEffect object.</param>
		public  void Update(PhotoEffect photoeffect)
		{
			if (photoeffect == null)
				throw new ArgumentNullException("photoeffect");

			SQLManagedConnection myConn = new SQLManagedConnection(Provider.DatabaseName);

			try
			{
				myConn.GetTextCommand("UPDATE be_PhotoEffect " +
									  "SET Title = @title, " +
									  "Effect = @effect, " +
									  "DateModified = @modifed " +
									  "WHERE PhotoEffectID = @id");

				myConn.AddInParameter("@id", DbType.Guid, photoeffect.Id);
				myConn.AddInParameter("@title", DbType.String, photoeffect.Title);
				myConn.AddInParameter("@effect", DbType.String, photoeffect.PhotoEffectName);
				myConn.AddInParameter("@modifed", DbType.DateTime, photoeffect.DateModified);

				if (!PhotoEffect.Items.Contains(photoeffect))
					PhotoEffect.Items.Add(photoeffect);				

				PhotoEffect.Items.Sort();
			}
			finally
			{
				myConn.CloseIfNonTransactional();
			}
		}

		/// <summary>
		/// Deletes a PhotoEffect
		/// </summary>
		/// <param name="link">Must be a valid PhotoEffect object.</param>
		public  void Delete(PhotoEffect photoeffect)
		{
			if (photoeffect == null)
				throw new ArgumentNullException("link");

			SQLManagedConnection myConn = new SQLManagedConnection(Provider.DatabaseName);

			try
			{
				myConn.GetTextCommand("DELETE FROM be_PhotoEffects WHERE PhotoEffectID = @id");
				myConn.AddInParameter("@id", DbType.Guid, photoeffect.Id);
				myConn.ExecuteNonQuery();

				if (PhotoEffect.Items.Contains(photoeffect))
					PhotoEffect.Items.Remove(photoeffect);
			}
			finally
			{
				myConn.CloseIfNonTransactional();
			}
		}

		/// <summary>
		/// Fills a sorted list of PhotoEffects.
		/// </summary>
		/// <returns>A List&lt;PhotoEffect&gt; of all PhotoEffects.</returns>
		public  List<PhotoEffect> Fill()
		{
			List<PhotoEffect> photoeffects = new List<PhotoEffect>();

			//for each entry found, cause a data load to occur - done this way so the reader is shut first!
			foreach (Guid guid in SQLUtil.GetGuids("SELECT PhotoEffectID FROM be_PhotoEffects", Provider.DatabaseName))
				photoeffects.Add(PhotoEffect.Load(guid));

			photoeffects.Sort();
			return photoeffects;
		}
	}
}
